package com.qf.controller;


import com.qf.entity.Employee;
import com.qf.enums.EmployerExceptionEnum;
import com.qf.service.EmployeeService;
import com.qf.utils.R;
import com.qf.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/main")
@Slf4j
public class MainController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/login-ui")
    public String loginUI(){
        return "redirect:/login.jsp";
    }

    @PostMapping("/login")
    @ResponseBody
    public ResultVO login(String loginName, String loginPassword, HttpSession session){
        if(loginName==null||loginPassword==null){
            log.info("【登陆功能】用户名或密码为必填项，岂能为空");
            return R.error(EmployerExceptionEnum.USER_PARAM_ERROR.getCode(),"用户名或密码为必填项，岂能为空");
        }
        Employee employee = employeeService.login(loginName, loginPassword);
        session.setAttribute("employee",employee);
        return R.ok();
    }
    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session){
        //清除session
        session.invalidate();
        return "redirect:/login.jsp";
    }
}
